import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../widget/my_dialog.dart';
import '../../services/user.dart';
class TopLogin extends StatefulWidget {
  TopLogin({this.userInfo});
  final Map<String,dynamic> userInfo;
  @override
  _TopLoginState createState() => _TopLoginState();
}

class _TopLoginState extends State<TopLogin> {
   bool isLogin=false;
   List<Map<String,dynamic>> currentUser=[
     {
       'name':'我的余额',
       'value':'0.00',
       'route':'/coupon'
     },
     {
       'name':'当前积分',
       'value':'0.00',
       'route':'/score_detail'
     },
     {
       'name':'优惠券',
       'value':'0',
       'route':'/coupon'
     },
   ];

  @override
  void initState() {
    super.initState();
    setState(() {
      isLogin=widget.userInfo.isEmpty?false:true;
    });
  }

   //退出登录
   Future<void> logout(context) async {
     MyDialog dialog = MyDialog();
     String result=await dialog.alertDialog(context);
     if(result=='ok'){
       await User.loginOut();
       User.loginOut();
       setState(() {
         this.isLogin = false;
       });
       Navigator.of(context).pushNamed('/login');
     }
     if(result=='cancle'){
       Fluttertoast.showToast(msg: '取消成功');
     }



   }

  Widget build(BuildContext context) {
    if(isLogin){
      return Container(
            height: 300.w,
            width: double.infinity,
            child: Stack(
              children: [
                Container(
                  alignment: Alignment.topCenter,
                  decoration: BoxDecoration(
                      color: Colors.red,
                      borderRadius: BorderRadius.only(bottomRight: Radius.circular(50),bottomLeft:Radius.circular(50) )
                  ),
                  height:250.w,
                  child:Container(
                    alignment: Alignment.topCenter,
                    height:100.w,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.start,
                      children: <Widget>[
                        Container(
                          margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
                          decoration: BoxDecoration(
                              border: Border.all(color: Colors.white, width: 2),
                              borderRadius: BorderRadius.all(Radius.circular(52))),
                          child: ClipOval(
                            child: Image.network(widget.userInfo['avatar'],width:100.w,height:100.w),
                          ),
                        ),
                        Expanded(
                          flex: 1,
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Text("用户名：${widget.userInfo["userName"]}",
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 32.sp)),
                              Text("普通会员",
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 24.sp)),
                            ],
                          ),
                        ),
                        Container(
                          child: isLogin
                              ?IconButton(
                            icon: Icon(Icons.settings,color: Colors.white,),
                            onPressed: (){
                              Navigator.pushNamed(context, '/setting');
                            },
                          ):Text(''),
                        )
                      ],
                    ),
                  ) ,
                ),
                Positioned(
                  bottom: 0,
                  left:15.w,
                  width:720.w,
                  height:150.w,
                  child: Container(
                    decoration: BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.all(Radius.circular(10.w))
                    ),
                    child: Row(
                      children: this.currentUser.map((value){
                        return Expanded(
                          child:GestureDetector(
                            onTap: (){
                              Navigator.of(context).pushNamed(value['route']);
                            },
                            child: Container(
                              height:100.w,
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  border:Border(
                                      right: BorderSide(color: Colors.grey[200])
                                  )
                              ),
                              child:Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Text('${value['name']}'),
                                  SizedBox(height: 10.w,),
                                  Text('${value['value']}')
                                ],
                              ) ,
                            ),
                          ) ,
                        );
                      }).toList()
                    ),
                  ),
                )
              ],
            )
        );
    }else{
      return Container(
          height: 250.h,
          width: double.infinity,
          decoration: BoxDecoration(
            color: Colors.red,
          ),
          child: Container(
            height:150.h,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.start,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.fromLTRB(10, 0, 10, 0),
                  decoration: BoxDecoration(
                      border: Border.all(color: Colors.white, width: 2),
                      borderRadius: BorderRadius.all(Radius.circular(50))),
                  child: ClipOval(
                    child: Icon(Icons.people, color: Colors.white, size: 60),
                  ),
                ),
                 Expanded(
                    flex: 1,
                    child: InkWell(
                      onTap: () {
                        Navigator.of(context).pushNamed('/login');
                      },
                      child: Container(
                        height: 150.h,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              "点击登录",
                              textAlign: TextAlign.left,
                              style: TextStyle(
                                  color: Colors.white, fontSize: 40.sp),
                            ),
                            SizedBox(
                              height: 20.h,
                            ),
                            Text('欢迎来到聚集集家居',
                                style: TextStyle(
                                    color: Colors.white, fontSize: 30.sp))
                          ],
                        ),
                      ),
                    ))

              ],
            ),
          )
      );
    }

  }
}
